package com.deyang.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ProgressBar;

import com.deyang.callback.DialogProgressLoadCallback;
import com.deyang.callback.IDialogSelectOptionCallBack;
import com.deyang.maillist.R;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.entity.DialogMenuItem;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.NormalListDialog;

import java.util.ArrayList;

public class DialogUtil {


    /**
     * 显示进度条的对话框
     *
     * @param message  提示消息
     * @param activity
     * @param callback 进度条回调 设置 进度
     * @return
     */
    public static void showProgressDialog(String message, Activity activity, DialogProgressLoadCallback callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(message);
        View progressView = LayoutInflater.from(activity).inflate(R.layout.dialog_progress_download, null);
        builder.setView(progressView);
        AlertDialog dialog = builder.create();
        ProgressBar pb_downLoad = progressView.findViewById(R.id.pb_downLoad);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
        callback.showProgressBar(dialog, pb_downLoad);
    }

    /**
     * 升级版本
     *
     * @param activity
     * @return
     */
    public static Dialog showOkOpenApk(final Activity activity, final String path) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("下载完成");
        builder.setMessage("下载完成，安装新版本");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
                ProjectFileUtil.install(activity, path);
            }
        });
        AlertDialog dialog = builder.create();

        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
        return dialog;
    }

    /**
     * 选择对话框
     *
     * @param activity
     * @param options
     * @param callBack
     */
    public static void showSelectOptionDialog(Activity activity, String title, String[] options, final IDialogSelectOptionCallBack callBack) {
        BounceTopEnter mBasIn = new BounceTopEnter();
        SlideBottomExit mBasOut = new SlideBottomExit();
        final ArrayList<DialogMenuItem> mMenuItems = new ArrayList<>();
        for (String option : options) {
            mMenuItems.add(new DialogMenuItem(option, R.drawable.select_options));
        }
        final NormalListDialog dialog = new NormalListDialog(activity, mMenuItems);
        dialog.isTitleShow(TextUtils.isEmpty(title) ? false : true)
                .title(TextUtils.isEmpty(title) ? "请选择" : title)
                .showAnim(mBasIn)//
                .titleBgColor(activity.getResources().getColor(R.color.select_dialog_title_bg))//
                .dismissAnim(mBasOut)//
                .show();
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                dialog.dismiss();
                callBack.onOperItemClick(i);
            }
        });
    }

    /**
     * 获取正在加载。。。 对话框
     *
     * @param context
     * @return
     */
    public static Dialog getLoadingDialog(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.loading, null);
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        return dialog;
    }

}
